/*
	File: C:/works/3d/games/mgt/panzer_2010/data/scripts/gate.nut
	Author: Astrofra
*/

		Include("data/scripts/globals.nut")
		Include("data/scripts/game_item.nut")

/*!
	@short	GenericGate
	@author	Astrofra
*/

//	Based on the GameItem class, this class handles a simple door
//	that opens when the player enters the trigger.
//	Once openned, the door will remain in this state.

class	GenericGate extends GameItem
{
	root_item	=	0
	gate_item	=	0

	door_pos	=	0

	closed		=	true

	audio		=	0

	/*!
		@short	OnUpdate
		Called during the scene update, each frame.
	*/
	function	OnUpdate(item)
	{
		base.OnUpdate(item)

		//	Update the physic position of the door item
		//	based on the command list (PATCH)
		if (!ItemIsCommandListDone(gate_item))
			ItemSetPhysicPosition(gate_item, ItemGetWorldPosition(gate_item))
	}

	function	PlayerEntersItem(item)
	{
		print("GenericGate::PlayerEntersItem()")
		if (closed)
			Open(item)
	}

	function	Open(item)
	{
		local	_x = door_pos.down.x,
				_y = door_pos.down.y

		local	str = ""
		str += "toposition 1.0," + _x.tostring() + "," + _y.tostring() + ",0;"
		str += "toposition 0.1," + _x.tostring() + "," + (_y - 0.10).tostring() + ",0;"
		str += "toposition 0.1," + _x.tostring() + "," + (_y + 0.05).tostring() + ",0;"
		str += "toposition 0.1," + _x.tostring() + "," + (_y - 0.025).tostring() + ",0;"
		str += "toposition 0.1," + _x.tostring() + "," + _y.tostring() + ",0;"

		ItemSetCommandList(gate_item, str)

		audio.PlaySfxDoorOpen()

		closed = false
		project_script.SetItemStateClosed(item, closed)
	}

	function	Close(item)
	{
		local	_y = door_pos.up.y

		local	str = ""
		str += "toposition 1.0," + _x.tostring() + "," + _y.tostring() + ",0;"
		str += "toposition 0.1," + _x.tostring() + "," + (_y - 0.10).tostring() + ",0;"
		str += "toposition 0.1," + _x.tostring() + "," + (_y + 0.05).tostring() + ",0;"
		str += "toposition 0.1," + _x.tostring() + "," + (_y - 0.025).tostring() + ",0;"
		str += "toposition 0.1," + _x.tostring() + "," + _y.tostring() + ",0;"

		ItemSetCommandList(gate_item, str)

		audio.PlaySfxDoorOpen()

		closed = true
		project_script.SetItemStateClosed(item, closed)
	}	

	/*!
		@short	OnSetup
		Called when the item is about to be setup.
	*/
	function	OnSetup(item)
	{
		base.OnSetup(item)
		print("GenericGate::OnSetup(" + ItemGetName(item) + ").")

		audio = project_script.audio.environment

		door_pos = { up = 0, down = 0 }
		root_item = item
		gate_item = ItemGetChild(item, "door")
		door_pos.up = ItemGetPosition(ItemGetChild(item, "door_up"))
		door_pos.down = ItemGetPosition(ItemGetChild(item, "door_down"))

		closed = project_script.GetItemStateClosed(item)

		if (closed)
			ItemSetPosition(gate_item, door_pos.up)
		else
			ItemSetPosition(gate_item, door_pos.down)
	}
}

//	Extension of the generic gate, this one requires
//	the player to own a specific key in his inventory

class	LockedGate	extends	GenericGate
{
	key_name	=	0
	key_list	=	["key_0", "key_1", "key_2", "key_3", "key_4", "key_5", "key_6"] 

	function	OnSetup(item)
	{
		base.OnSetup(item)
		print("LockedGate::OnSetup(" + ItemGetName(item) + ").")

		local	key_item, key_slot, n
		local	key_item_list 
		local	parent = ItemGetParent(item)
		
		print("LockedGate::OnSetup() found parent : " + ItemGetName(parent))

		n = 0
		while(n < key_list.len())
		{
			key_item = ItemGetChild(parent, key_list[n])
			if (ObjectIsValid(key_item))
				break
			n++
		}

		key_name = ItemGetName(key_item)
		key_slot = ItemGetChild( ItemGetChild(item, "door"), "key_slot")
		ItemSetParent(key_item, key_slot)
		ItemSetPosition(key_item, Vector(0,0,0))
		ItemSetRotation(key_item, Vector(0,0,0))
		print("LockedGate::OnSetup(), gate opens with key : '" + key_name + "'.")
	}

	function	OnUpdate(item)
	{
		base.OnUpdate(item)
	}

	function	PlayerEntersItem(item)
	{
		print("LockedGate::PlayerEntersItem()")
		if (closed && project_script.inventory.FindItem(key_name))
		{
			Open(item)
			project_script.inventory.RemoveItem(key_name)
			project_script.hud_ui.UpdateInventory(project_script.inventory)
			project_script.audio.player.PlaySfxUseKey()			
		}
	}
}
